<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Week_prize_entity{
	var $id = 0;
	var $tournament_id = 0;
	var $rank = 0;
	var $title = "";
	var $prize = "";
	var $image = "";
	var $winner_id = 0;
	var $courier_name = "";
	var $shipping_date = 0;
	var $tracking_no = "";
	var $description = "";
	var $logo = "";
	var $value = "";
	var $starttime = "";
	var $endtime = "";
	var $enable = "";
	var $status = "";
	var $is_category = "";
	var $category_id = 0;
	var $delivery_status = '';
	var $proof = '';
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_WEEKLY_PRIZE)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->tournament_id = $result[0]->tournament_id;
			$this->rank = $result[0]->rank;
			$this->title = $result[0]->title;
			$this->prize = $result[0]->prize;
			$this->image = $result[0]->image;
			$this->winner_id = $result[0]->winner_id;
			$this->courier_name = $result[0]->courier_name;
			$this->shipping_date = $result[0]->shipping_date;
			$this->tracking_no = $result[0]->tracking_no;
			$this->description = $result[0]->description;
			$this->logo = $result[0]->logo;
			$this->value = $result[0]->value;
			$this->starttime = $result[0]->starttime;
			$this->endtime = $result[0]->endtime;
			$this->enable = $result[0]->enable;
			$this->status = $result[0]->status;
			$this->is_category = $result[0]->is_category;
			$this->category_id = $result[0]->category_id;
			$this->delivery_status = $result[0]->delivery_status;
			$this->proof = $result[0]->proof;
			
			// Create folder if not exists
			$directory = "./uploads/default/prizeweek";
			if(!file_exists($directory)){
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			
			return $this;
		}
		return false;
	}
	
	function setTournamentId($input){ $this->tournament_id=$input; }
	function setRank($input){ $this->rank=$input;}
	function setTitle($input){ $this->title=$input; }
	function setPrizeName($input){ $this->prize=$input; }
	function setImage($input){ $this->image=$input;}
	function setWinnerId($input){ $this->winner_id=$input; }
	function setCourierName($input){ $this->courier_name=$input;}
	function setShippingDate($input){ $this->shipping_date=$input;}
	function setTrackingNo($input){ $this->tracking_no=$input;}
	function setDescription($input){ $this->description=$input;}
	function setLogo($input){ $this->logo=$input; }
	function setValue($input){ $this->value=$input; }
	function setStartTime($input){ $this->starttime=$input; }
	function setEndTime($input){ $this->endtime=$input; }
	function setEnable($input){ $this->enable=$input;}
	function setStatus($input){ $this->status=$input; }	
	function setIsCategory($input){ $this->is_category=$input; }
	function setCategoryId($input){ $this->category_id=$input; }
	function setDeliveryStatus($input){ $this->delivery_status=$input; }
	function setProof($input){ $this->proof=$input; }
	
	function getId(){ return $this->id; }
	function getTournamentId(){ return $this->tournament_id; }
	function getRank(){ return $this->rank;}
	function getTitle(){ return $this->title; }
	function getPrizeName(){ return $this->prize;}
	function getImage(){ return $this->image;}
	function getWinnerId(){ return $this->winner_id;}
	function getCourierName(){ return $this->courier_name;}
	function getShippingDate(){ return $this->shipping_date;}
	function getTrackingNo(){ return $this->tracking_no;}
	function getDescription(){ return $this->description;}
	function getLogo(){ return $this->logo; }
	function getValue(){ return $this->value; }
	function getStartTime(){ return $this->starttime; }
	function getEndTime(){ return $this->endtime; }
	function getEnable(){ return $this->enable;}
	function getStatus(){ return $this->status; }	
	function getIsCategory(){ return $this->is_category; }
	function getCategoryId(){ return $this->category_id; }
	function getDeliveryStatus(){ return $this->delivery_status; }
	function getProof(){ return $this->proof; }
	
	function getFolderFileStorage(){ 
		return "./uploads/default/prizeweek/"; 
	}
	
	function getPrizeImage(){
		return $this->getFolderFileStorage().$this->getImage();
	}
	
	function getLogoImage(){
		return $this->getFolderFileStorage().$this->getLogo();
	}
	
	function getProofImage(){
		return $this->getFolderFileStorage().$this->getProof();
	}
	
	function getProofTargetFileUrl(){
		return site_url()."uploads/default/prizeweek/".$this->getProof();
	}
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('rank', $this->rank);
		$ci->db->set('title', $this->title);
		$ci->db->set('prize', $this->prize);
		$ci->db->set('image', $this->image);
		$ci->db->set('winner_id', $this->winner_id);
		$ci->db->set('courier_name', $this->courier_name);
		$ci->db->set('shipping_date', $this->shipping_date);
		$ci->db->set('tracking_no', $this->tracking_no);
		$ci->db->set('description', $this->description);
		$ci->db->set('logo', $this->logo);
		$ci->db->set('value', $this->value);
		$ci->db->set('starttime', $this->starttime);
		$ci->db->set('endtime', $this->endtime);
		$ci->db->set('enable', $this->enable);
		$ci->db->set('status', $this->status);
		$ci->db->set('is_category', $this->is_category);
		$ci->db->set('category_id', $this->category_id);
		$ci->db->set('delivery_status', $this->delivery_status);
		$ci->db->set('proof', $this->proof);
		
		$ci->db->where('id', $this->id)->update(TBL_WEEKLY_PRIZE);
	}
	
	function createNewEntity(){
		$ci=& get_instance();
		
		$ci->db->set('title','');
		$ci->db->insert(TBL_WEEKLY_PRIZE);
		
		return $ci->db->insert_id();
	}
	
	function getArrayListOfPrizeCategories($tournament_id){
		$ci=& get_instance();
		return $ci->db->where( array('tournament_id'=> $tournament_id ,'is_category' => 'yes'))
		->order_by('id','asc')->get(TBL_WEEKLY_PRIZE)->result();
	}
	
	function getFinishedCategoriesOfTournament($tournament_id){
		$ci=& get_instance();
		return $ci->db->where( array('tournament_id'=> $tournament_id ,'is_category' => 'yes','status' => 'Finished'))
		->order_by('id','asc')->get(TBL_WEEKLY_PRIZE)->result();
	}
	
	function getArrayListOfPrize($cat_id){
		$ci=& get_instance();
		return $ci->db->where( array('category_id'=> $cat_id ))
		->order_by('rank','asc')->get(TBL_WEEKLY_PRIZE)->result();
	}
	
	function changeStatusPrizeWeekRealTime(){
		$ci=& get_instance();
		$ci->db->where("is_category LIKE 'yes' AND status LIKE 'Upcoming' AND starttime <=".time())->update(TBL_WEEKLY_PRIZE, array('status'=>'Living'));
		$ci->db->where("is_category LIKE 'yes' AND status LIKE 'Living' AND endtime <=".time())->update(TBL_WEEKLY_PRIZE, array('status'=>'Closed'));		
	}
	
	function getLiveWeekPrizeOfTournament($tournament_id){
		$ci=& get_instance();
		$cat_rs = $ci->db->where("is_category LIKE 'yes' AND status LIKE 'Living' AND enable LIKE 'yes' AND tournament_id=$tournament_id")->get(TBL_WEEKLY_PRIZE)->result();
		if($cat_rs){
			$result = $ci->db->where(array('category_id'=>$cat_rs[0]->id))->order_by('rank','asc')->get(TBL_WEEKLY_PRIZE)->result();
			return array('cat_info'=>$cat_rs,'prize_array'=>$result); 
		}
		return null;
	}
	
	function getArrayWeeklyRanksOfTournament($tournament_id,$cat_id){
		$ci=& get_instance();
		
		$cat_res = $ci->db->where('id',$cat_id)->get(TBL_WEEKLY_PRIZE)->result();
		if(!$cat_res) return null;
		
		$starttime = $cat_res[0]->starttime;
		$endtime = $cat_res[0]->endtime;
		
		$strQR = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET_MANUAL." 
									WHERE bet_time >= $starttime AND bet_time <= $endtime AND result='won' AND tournament_id=$tournament_id
									GROUP BY user_id ORDER BY total DESC"; 
		$res1 = $ci->db->query($strQR )->result();
		$res_rs1 = array();
		foreach($res1 as $k=>$v){
			$res_rs1[$v->user_id] = $v->total;
		}
			
		$strQR2 = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET." 
									WHERE bet_time >= $starttime AND bet_time <= $endtime AND result='won' AND tournament_id=$tournament_id
									GROUP BY user_id ORDER BY total DESC"; 
		$res2 = $ci->db->query($strQR2 )->result();
		$res_rs2 = array();
		foreach($res2 as $k=>$v){
			$res_rs2[$v->user_id] = $v->total;
		}
		
		$strQR3 = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET_TOURNAMENT." 
									WHERE bet_time >= $starttime AND bet_time <= $endtime AND result='won' AND tournament_id=$tournament_id
									GROUP BY user_id ORDER BY total DESC"; 
		$res3 = $ci->db->query($strQR3 )->result();
		$res_rs3 = array();
		foreach($res3 as $k=>$v){
			$res_rs3[$v->user_id] = $v->total;
		}
		
		$strQR4 = "SELECT user_id, sum(point_get) as total FROM ".TBL_BET_MANUAL_TOURNAMENT." 
									WHERE bet_time >= $starttime AND bet_time <= $endtime AND result='won' AND tournament_id=$tournament_id
									GROUP BY user_id ORDER BY total DESC"; 
		$res4 = $ci->db->query($strQR4 )->result();
		$res_rs4 = array();
		foreach($res4 as $k=>$v){
			$res_rs4[$v->user_id] = $v->total;
		}
		
		/*//////////////////////////////////////////////////////////////////////////////*/
		$ranksArray = array();
		foreach($res_rs1 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		foreach($res_rs2 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		foreach($res_rs3 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		foreach($res_rs4 as $k=>$v){
			if(isset($ranksArray[$k])){
				$ranksArray[$k] += $v;
			}else{
				$ranksArray[$k] = $v;
			}
		}
		/*
		$array_user_id = ( array_keys($res_rs1) + array_keys($res_rs2) + array_keys($res_rs3) + array_keys($res_rs4));
		
		$ranksArray = array();
		foreach($array_user_id as $k=>$v){
			$ranksArray[$v] = 0;
			if(isset($res_rs1[$v])){
				$ranksArray[$v] += $res_rs1[$v];
			}
			if(isset($res_rs2[$v])){
				$ranksArray[$v] += $res_rs2[$v];
			}
			if(isset($res_rs3[$v])){
				$ranksArray[$v] += $res_rs3[$v];
			}
			if(isset($res_rs4[$v])){
				$ranksArray[$v] += $res_rs4[$v];
			}
		}
		*/
		
		$str = "SELECT user_id, sum(point) as total FROM ".TBL_CONVERSION." 
									WHERE `time` >= $starttime AND `time` <= $endtime AND tournament_id=$tournament_id
									GROUP BY user_id ";
		$convertWeek = $ci->db->query($str)->result();
		foreach($convertWeek as $k=>$v){
			if(isset($ranksArray[$v->user_id])){
				$ranksArray[$v->user_id] = $ranksArray[$v->user_id] - $v->total;
			}
		}
		
		foreach($ranksArray as $k=>$v){
			if($v <=0){
				unset($ranksArray[$k]);
			}
		}
		
		asort($ranksArray);
		$ranksArray = array_reverse($ranksArray,true);
		return $ranksArray;
		//$tmp = array_chunk($ranksArray, $record_per_page, true);
		//return $tmp[$page_index] ;
	}
	
	function getPersonWithARank($rank_index=1,$tournament_id,$cat_id){
		$tmp = 1;
		$info = array();
		$ranksArray = $this->getArrayWeeklyRanksOfTournament($tournament_id,$cat_id);
		if(sizeof($ranksArray) < $rank_index){return null;}
		
		foreach( $ranksArray as $k=>$v ){
			if($tmp == $rank_index){
				$info = array('user_id'=>$k ,'point' => $v);
				break;
			}
			$tmp++;
		}
		return $info;
	}
	
	function getUserObjectWithRank($rank_index=1,$tournament_id,$cat_id){
		$ci=& get_instance();
		$info = $this->getPersonWithARank($rank_index,$tournament_id,$cat_id);
		if($info){
			return clone_( $ci->user_entity->init('id',$info['user_id']) );
		}
		return false;
	}
	
	function getPrizeObjectWithARank($rank_index=1,$cat_id){
		$res = $this->getArrayListOfPrize($cat_id);
		$tmp = 0;
		foreach($res as $item){
			if($item->rank == $rank_index){
				$tmp = $item->id;
			}
		}
		return $this->init('id',$tmp);
	}
	
	//endclass
}
